//
//  PartyTableViewController.m
//  OneDay
//
//  Created by oneDay on 15/10/16.
//  Copyright (c) 2015年 oneDay All rights reserved.
//

#import "PartyTableViewController.h"
#import "PartyTableViewCell.h"
#import "PartyDetailViewController.h"

@interface PartyTableViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation PartyTableViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView.header endRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = 1;
    NSString *partyUrl = [kParty stringByAppendingString:[NSString stringWithFormat:@"&page=%ld&lat=40.030613",self.page]];

    self.dataArray = [NSMutableArray array];
  [[OneDayManager shareManager] requestFirstPartyDataWithUrl:partyUrl block:^(NSMutableArray *array) {
      [self.dataArray addObjectsFromArray:array];
      PartyModel *model = [self.dataArray firstObject];
      if ([model.item_type isEqualToString:@"event"]) {
          [self.dataArray removeObject:model];
      }
      [self.tableView reloadData];
  }];
  
    [self setUp];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = nil;
    PartyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[PartyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    // 取消分割线
    self.tableView.separatorStyle = NO;
    // 取消选中的颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    PartyModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 320;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PartyDetailViewController *detailVC = [[PartyDetailViewController alloc] init];
    PartyModel *model = self.dataArray[indexPath.row];
    detailVC.partyMdoel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)setUp
{
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.tableView.footer = [MJRefreshBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    // 进入刷新状态
    [self.tableView.header beginRefreshing];
}
- (void)footerRefresh
{
    _page++;
     NSString *partyUrl = [kParty stringByAppendingString:[NSString stringWithFormat:@"&page=%ld&lat=40.030613",self.page]];
    [[OneDayManager shareManager] requestFirstPartyDataWithUrl:partyUrl block:^(NSArray *array) {
        [self.dataArray addObjectsFromArray:array];
        [self.tableView reloadData];
    }];
    [self.tableView.footer endRefreshing];
    
}

- (void)headerRefresh
{
    self.page = 1;
    // 在这个方法中请求数据
    // 结束刷新状态
    
    [self.tableView.header endRefreshing];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
